package com.zy.fallback;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.zy.feign.ProductFeign;
import com.zy.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author xyz
 */
@Slf4j
public class SentinelFallback {

    public static Result fallback(Throwable e) {
        String msg = e instanceof FlowException ? "流控异常：您访问的太快或当前访问人数过多，请稍后重试.." :
                     e instanceof DegradeException ? "熔断异常：服务器响应超时或失败次数过多，请稍后重试.." :
                     e instanceof AuthorityException ? "授权异常：您无权访问该资源，请联系管理员.." :
                     e instanceof SystemBlockException ? "系统异常：系统限制，sentinel熔断降级.." :
                     "未知异常：服务器未知异常，请稍后重试..";
        System.out.println("msg: " + msg);
        return Result.fail(-1, msg);
    }

    public static Result fallback(String name, Integer age, Throwable e) {
        String msg = "未知异常：服务器未知异常，请稍后重试..";
        if (e instanceof ParamFlowException) {
            msg = "热点异常：访问受限";
        }
        System.out.println("msg: " + msg + ":" + e);
        return Result.fail(-1, msg);
    }

}
